﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Reflection;
using System.Web.UI.WebControls;

namespace VNTG.CustomFieldTypes
{
    public class AdvancedComputedFieldEditor : UserControl, IFieldEditor
    {
        #region WebControls
        protected InputFormTextBox iftxtFieldRefs;
        protected InputFormTextBox iftxtDisplayPattern;
        #endregion

        #region IFieldEditor Members
        public bool DisplayAsNewSection
        {
            get { return false; }
        }

        public void InitializeWithField(SPField field)
        {
            // If it's no postback (first time loading on Edit Field)
            if (!IsPostBack)
            {
                AdvancedComputedField myField = field as AdvancedComputedField;

                // For new fields this is null so no initialisation based on the field (makes sense)
                if (null != myField)
                {
                    iftxtFieldRefs.Text = myField.GetInnerXmlForNode("FieldRefs");
                    iftxtDisplayPattern.Text = myField.GetInnerXmlForNode("DisplayPattern");
                }
            }
        }     

        public void OnSaveChange(SPField field, bool isNewField)
        {
            AdvancedComputedField myField = field as AdvancedComputedField;

            if (null != myField)
            {
                myField.SetInnerXmlForNode("FieldRefs", iftxtFieldRefs.Text);
                myField.SetInnerXmlForNode("DisplayPattern", iftxtDisplayPattern.Text);
            }
        }
        #endregion
    }
}
